package org.example.cet.demos.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.dto.HomeworkDto;
import org.example.cet.demos.domain.po.Homework;
import org.example.cet.demos.domain.query.HomeworkQuery;
import org.example.cet.demos.mapper.HomeworkMapper;
import org.example.cet.demos.service.IHomeworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@Service
@RequiredArgsConstructor

public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements IHomeworkService {

    public PageBean getPage(HomeworkQuery homeworkQuery) {
        Page<Homework> page = new Page<>(homeworkQuery.getCurrentPage(), homeworkQuery.getPageSize());
        lambdaQuery().like(homeworkQuery.getId()!=null,Homework::getId, homeworkQuery.getId())
                .like(!StrUtil.isBlankIfStr(homeworkQuery.getClassId()), Homework::getClassId, homeworkQuery.getClassId())
                .eq(homeworkQuery.getType()!=null,Homework::getType, homeworkQuery.getType())
                .like(!StrUtil.isBlankIfStr(homeworkQuery.getTitle()), Homework::getTitle, homeworkQuery.getTitle())
                .like(!StrUtil.isBlankIfStr(homeworkQuery.getQuestionId()), Homework::getQuestionId, homeworkQuery.getQuestionId())
                .eq(homeworkQuery.getDueDate() != null, Homework::getDueDate, homeworkQuery.getDueDate())
                .eq(homeworkQuery.getCreateTime() != null, Homework::getCreateTime, homeworkQuery.getCreateTime())
                .page(page);
        PageBean pageBean = new PageBean();
        pageBean.setTotal(page.getTotal());
        pageBean.setRows(page.getRecords());
        return pageBean;
    }

    @Autowired
    private HomeworkMapper homeworkMapper;
    @Override
    public List<HomeworkDto> getList(){
        return homeworkMapper.getList();
    }

}
